import 'package:flutter/material.dart';
import 'package:ref/ref.dart';
import 'state.dart';

class TestKey extends StatelessWidget {
  const TestKey({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text('-----测试对Ref对象的属性代理-----'),
        Wrap(
          children: [
            MvvmDemo01(),
            MvvmDemo02(),
          ],
        )
      ],
    );
  }
}

// -------------------------------------------------------------------------------------------------------
class MvvmDemo01 extends StatefulWidget {
  const MvvmDemo01({Key? key}) : super(key: key);
  @override
  _MvvmDemo01State createState() => _MvvmDemo01State();
}

class _MvvmDemo01State extends State<MvvmDemo01> {
  add() {
    keyInfo.set(() {
      keyInfo.value['info']['name'] = keyInfo.value['info']['name'] + '流弊！';
      keyInfo.value['arr'][0] = keyInfo.value['arr'][0] + 'x';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width / 2,
      color: Colors.black.withOpacity(0.1),
      padding: EdgeInsets.all(5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          RefBuilder(
            () => Column(
              children: [
                Text(
                  keyInfo.value['info']['name'],
                  style: TextStyle(fontSize: 14),
                ),
                Text(
                  keyInfo.value['arr'][0],
                  style: TextStyle(fontSize: 20),
                )
              ],
            ),
          ),
          TextButton(onPressed: add, child: Text('发力')),
        ],
      ),
    );
  }
}

// ---------------------------------------------------------------------------------------------------
class MvvmDemo02 extends StatefulWidget {
  const MvvmDemo02({Key? key}) : super(key: key);
  @override
  _MvvmDemo02State createState() => _MvvmDemo02State();
}

class _MvvmDemo02State extends State<MvvmDemo02> {
  RefKey arr = RefKey(keyInfo, 'arr.0');
  RefKey name = RefKey(keyInfo, 'info.name');

  add() {
    keyInfo.set(() {
      // keyInfo.value['info']['name'] = '老子';
      // keyInfo.value['arr'][0] = '苹果';
      name.value = '老子';
      arr.value = '苹果';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width / 2,
      color: Colors.black.withOpacity(0.2),
      padding: EdgeInsets.all(5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          RefBuilder(
            () => Column(
              children: [
                Text(
                  // keyInfo.value['info']['name'],
                  name.value,
                  style: TextStyle(fontSize: 14),
                ),
                Text(
                  // keyInfo.value['arr'][0],
                  arr.value,
                  style: TextStyle(fontSize: 20),
                )
              ],
            ),
          ),
          TextButton(onPressed: add, child: Text('还原')),
        ],
      ),
    );
  }
}
